<template>
  <div>
    <template>
      <el-button icon="el-icon-edit" @click="dialogVisible = true">点击打开 Dialog</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="teachId" label="教材ID" width="180"></el-table-column>
        <el-table-column prop="teachTitle" label="教材标题" width="180"></el-table-column>
        <el-table-column prop="teachContent" label="教材内容" width="180"></el-table-column>
        <el-table-column prop="createTime" label="发布时间" width="180"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button>
            <el-button size="mini" type="danger" @click="deleteAll(scope.$index, scope.row)">删除教材</el-button>
            <!--            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">查看教学代码</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template>
      <el-pagination small layout="total, prev, pager, next" :page-count="pages" :page-size="size"
        :current-page="current" :total="total" @current-change="pageheigerGb">
      </el-pagination>
    </template>



    <div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="教材标题" prop="title">
            <el-input type="text" v-model="ruleForm.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="教材内容" prop="content">
            <el-input type="text" v-model="ruleForm.content" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [],
        total: 0,
        size: 0,
        pages: 0,
        current: 0,
        dialogVisible: false,
        ruleForm: {
          title: "",
          content: ""
        }
      }
    },
    methods: {
      deleteAll(index, row) {
        console.log(index, row);
        let data = new FormData();
        data.append("listParamVo", row.teachId)
        this.$http.post("http://localhost:8001/tbTeach/deleteTeach", data).then(res => {
          if (res.data.code == 200) {
              alert("删除成功")
          }
        })
      },
      handleDelete(index, row) {
        console.log(index, row);
        sessionStorage.setItem("teachId", row.teachId)
        this.$router.push("codeing")
      },
      add() {
        let outlineId = sessionStorage.getItem("outlineId");
        let param = {
          "teachTitle": this.ruleForm.title,
          "teachContent": this.ruleForm.content,
          "outlineId": outlineId
        }
        this.$http.post("http://localhost:8001/tbTeach/insertTeach", param).then(res => {
          if (res.data.code === 200) {
            this.dialogVisible = false
          }
        })
      },
      pageheigerGb(val) {
        let outlineId = sessionStorage.getItem("outlineId");
        let param = {
          "current": val,
          "size": 3,
          "paramMap": {
            "outlienID": outlineId
          }
        }
        this.$http.post("http://localhost:8001/tbTeach/selectTeach", param).then(res => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records;
            this.total = parseInt(res.data.data.total)
            this.size = parseInt(res.data.data.size)
            this.pages = parseInt(res.data.data.pages)
            this.current = parseInt(res.data.data.current)
          }
        })
      }
    },
    mounted() {
      let outlineId = sessionStorage.getItem("outlineId");
      let param = {
        "current": 1,
        "size": 3,
        "paramMap": {
          "outlienID": outlineId
        }
      }
      if (outlineId === null || outlineId == 0) {
        alert("请选择对应大纲")
        this.$router.push("outline")
      } else {
        this.$http.post("http://localhost:8001/tbTeach/selectTeach", param).then(res => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records;
            this.total = parseInt(res.data.data.total)
            this.size = parseInt(res.data.data.size)
            this.pages = parseInt(res.data.data.pages)
            this.current = parseInt(res.data.data.current)
          }
        })
      }

    }
  }
</script>
